from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json

mcp = FastMCP("SDL-Config MCP Server")

"""
面向软件：sdl-config

所有 MCP tool 列表：
1. sdl_config_version - 获取 SDL 版本信息
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

2. sdl_config_cflags - 获取编译 SDL 程序所需的编译器标志
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

3. sdl_config_libs - 获取编译 SDL 程序所需的链接器标志
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

4. sdl_config_static_libs - 获取静态链接 SDL 程序所需的链接器标志
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

5. sdl_config_prefix - 获取 SDL 的安装前缀路径
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

6. sdl_config_exec_prefix - 获取 SDL 的可执行文件前缀路径
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

7. sdl_config_help - 获取 sdl-config 帮助信息
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
"""

@mcp.tool()
def sdl_config_version() -> dict:
    """获取 SDL 版本信息
    
    返回：
        dict: 包含命令执行结果的统一 JSON 结构
    """
    command = ["sdl-config", "--version"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def sdl_config_cflags() -> dict:
    """获取编译 SDL 程序所需的编译器标志
    
    返回：
        dict: 包含命令执行结果的统一 JSON 结构
    """
    command = ["sdl-config", "--cflags"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def sdl_config_libs() -> dict:
    """获取编译 SDL 程序所需的链接器标志
    
    返回：
        dict: 包含命令执行结果的统一 JSON 结构
    """
    command = ["sdl-config", "--libs"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def sdl_config_static_libs() -> dict:
    """获取静态链接 SDL 程序所需的链接器标志
    
    返回：
        dict: 包含命令执行结果的统一 JSON 结构
    """
    command = ["sdl-config", "--static-libs"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def sdl_config_prefix() -> dict:
    """获取 SDL 的安装前缀路径
    
    返回：
        dict: 包含命令执行结果的统一 JSON 结构
    """
    command = ["sdl-config", "--prefix"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def sdl_config_exec_prefix() -> dict:
    """获取 SDL 的可执行文件前缀路径
    
    返回：
        dict: 包含命令执行结果的统一 JSON 结构
    """
    command = ["sdl-config", "--exec-prefix"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def sdl_config_help() -> dict:
    """获取 sdl-config 帮助信息
    
    返回：
        dict: 包含命令执行结果的统一 JSON 结构
    """
    command = ["sdl-config", "--help"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

if __name__ == "__main__":
    mcp.run()